package com.example.lol;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.lol.adapter.NewsAdapter;
import com.example.lol.base.BaseActivity;
import com.example.lol.ui.me.MeMainFragment;
import com.example.lol.ui.news.FriendFragment;
import com.example.lol.ui.news.GoodfriendFragment;

import com.example.lol.ui.news.MeActivityFragment;
import com.example.lol.ui.news.NewsMainFragment;
import com.example.lol.ui.news.ShoppingmallFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import static java.security.AccessController.getContext;

public class MainActivity extends BaseActivity {
    private DrawerLayout mDrawerLayout;


    private View rootContentView;
    private View drawerBtn;

    private BottomNavigationView mBottomNavigationView;
    private final SparseArray<Fragment> mFragmentCache = new SparseArray<>();

    @Override
    protected int contentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mDrawerLayout = findViewById(R.id.drawer_layout);

        rootContentView = findViewById(R.id.home_main);
        drawerBtn = findViewById(R.id.home_page_top_left_nav);
        mBottomNavigationView = findViewById(R.id.home_main_bottom_nav);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void bindData() {
        super.bindData();
        mBottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            Fragment fragment = getContent(item);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.home_page_middle_totle, fragment)
                    .commit();
            return true;
        });
        mBottomNavigationView.setSelectedItemId(R.id.home_page_bottom_strategy);
    }

    private Fragment getContent(MenuItem menuItem) {
        int id = menuItem.getItemId();
        Fragment fragment = mFragmentCache.get(id);
        if (fragment == null) {
            fragment = nf(menuItem);
            mFragmentCache.put(id, fragment);
        }
        return fragment;
    }

    private Fragment nf(MenuItem menuItem) {
        Fragment fragment;
        switch (menuItem.getItemId()){
            case R.id.home_page_bottom_strategy:
                fragment = new NewsMainFragment();
                break;
            case R.id.home_page_bottom_friend:
                fragment = new FriendFragment();
                break;
            case R.id.home_page_bottom_googfriend:
                fragment = new GoodfriendFragment();
                break;
            case R.id.home_page_bottom_shoppingmall:
                fragment = new ShoppingmallFragment();
                break;
            case R.id.home_page_bottom_me:
                fragment = new MeMainFragment();
                break;
            default:
                fragment = new NewsMainFragment();
        }
        return fragment;
    }

    @Override
    protected void bindEvent() {

        drawerBtn.setOnClickListener(v -> mDrawerLayout.openDrawer(GravityCompat.START));


        // 扫描
        ImageView LeftScan = findViewById(R.id.left_nav_head_scan);
        LeftScan.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "点击扫描", Toast.LENGTH_SHORT).show();
            }
        });

        //查看更多
        ImageView LeftLookmore = findViewById(R.id.left_nav_head_lookmore);
        LeftLookmore.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "查看更多", Toast.LENGTH_SHORT).show();
            }
        });

        //头像
        LinearLayout LeftUserHead = findViewById(R.id.left_nav_user_head);
        LeftUserHead.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "用户头像", Toast.LENGTH_SHORT).show();
            }
        });

        //用户名
        TextView LeftUserName = findViewById(R.id.left_nav_head_user_name);
        LeftUserName.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "用户名", Toast.LENGTH_SHORT).show();
            }
        });

        //等级
        TextView LeftLevel = findViewById(R.id.left_nav_head_level);
        LeftLevel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "用户等级", Toast.LENGTH_SHORT).show();
            }
        });

        //性别
        ImageView LeftSex = findViewById(R.id.left_nav_head_sex);
        LeftSex.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "用户性别", Toast.LENGTH_SHORT).show();
            }
        });

        //个性签名
        TextView LeftSign = findViewById(R.id.left_nav_head_sign);
        LeftSign.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "个性签名", Toast.LENGTH_SHORT).show();
            }
        });

        //我的评论
        ImageView LeftCommentImg = findViewById(R.id.left_nav_menu_comment_img);
        LeftCommentImg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "我的评论", Toast.LENGTH_SHORT).show();
            }
        });

        TextView LeftComment = findViewById(R.id.left_nav_menu_comment);
        LeftComment.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "我的评论", Toast.LENGTH_SHORT).show();
            }
        });

        //与我相关
        ImageView LeftRelateImg = findViewById(R.id.left_nav_menu_relate_img);
        LeftRelateImg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "与我相关", Toast.LENGTH_SHORT).show();
            }
        });

        TextView LeftRelate = findViewById(R.id.left_nav_menu_relate);
        LeftRelate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "与我相关", Toast.LENGTH_SHORT).show();
            }
        });

        //我的订单
        ImageView LeftOrderImg = findViewById(R.id.left_nav_menu_order_img);
        LeftOrderImg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "我的订单", Toast.LENGTH_SHORT).show();
            }
        });

        TextView LeftOrder = findViewById(R.id.left_nav_menu_order);
        LeftOrder.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "我的订单", Toast.LENGTH_SHORT).show();
            }
        });

        //我的任务
        ImageView LeftTaskImg = findViewById(R.id.left_nav_menu_task_img);
        LeftTaskImg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "我的任务", Toast.LENGTH_SHORT).show();
            }
        });

        TextView LeftTask = findViewById(R.id.left_nav_menu_task);
        LeftTask.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "我的任务", Toast.LENGTH_SHORT).show();
            }
        });

        //我的收藏
        ImageView LeftCollectionImg = findViewById(R.id.left_nav_menu_collection_img);
        LeftCollectionImg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "我的收藏", Toast.LENGTH_SHORT).show();
            }
        });

        TextView LeftCollection = findViewById(R.id.left_nav_menu_collection);
        LeftCollection.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "我的收藏", Toast.LENGTH_SHORT).show();
            }
        });

        //活动中心
        ImageView LeftActImg = findViewById(R.id.left_nav_menu_act_img);
        LeftActImg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "活动中心", Toast.LENGTH_SHORT).show();
            }
        });

        TextView LeftAct = findViewById(R.id.left_nav_menu_act);
        LeftAct.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "活动中心", Toast.LENGTH_SHORT).show();
            }
        });

        //信用分
        ImageView LeftCreditImg = findViewById(R.id.left_nav_menu_credit_img);
        LeftCreditImg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "信用分", Toast.LENGTH_SHORT).show();
            }
        });

        TextView LeftCredit = findViewById(R.id.left_nav_menu_credit);
        LeftCredit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "信用分", Toast.LENGTH_SHORT).show();
            }
        });

        //设置
        LinearLayout LeftSetting = findViewById(R.id.left_nav_setting);
        LeftSetting.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, SettingActivity.class);
                startActivity(intent);
            }
        });

        mDrawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                float diff = drawerView.getWidth() + drawerView.getX();
                rootContentView.setTranslationX(diff);
            }
        });


    }
}
